const sharp = require("sharp");

const fs = require("fs");

const path = require("path");

const sourcePath = path.join("ws");
fs.existsSync(sourcePath);
if (!sourcePath) {
  fs.mkdirSync(localPath, { recursive: true });
}

fs.readdirSync(sourcePath).forEach(async (file) => {
  const image = path.join(sourcePath, file);
  const localPath = path.join("ws-output");
  const prepareFinish = fs.existsSync(localPath);
  if (!prepareFinish) {
    fs.mkdirSync(localPath, { recursive: true });
  }
  const output = path.join(localPath, file.replace(".png", ".jpg"));
  await sharp(image)
    .resize(300) // 设置图片宽度为 200px
    .jpeg({ quality: 70 }) // 设置图片质量为 80%
    .toBuffer()
    .then(async (data) => {
      fs.writeFileSync(output, data);
    })
    .catch((err) => {
      console.log("Err: " + output);
    });

  console.log(output + " : done");
});
